Create a centralized machine_message module
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 28 Sep 2016 16:54:35 +0000 (19:54 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 2 Oct 2016 14:28:26 +0000 (17:28 +0300)
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/util/machine_message.rs [new file with mode: 0644]
src/cargo/util/mod.rs
tests/build.rs

index 7d423dd8dcb3f957e4cc5de9db28c152a2319a05..0b321c0c9c43ddee1f6507da44ec543540a35436 100644 (file)
@@ -10,7 +10,7 @@ use rustc_serialize::json;
 use core::{Package, PackageId, PackageSet, Target, Resolve};
 use core::{Profile, Profiles, Workspace};
 use core::shell::ColorConfig;
-use util::{self, CargoResult, human};
+use util::{self, CargoResult, human, machine_message};
 use util::{Config, internal, ChainError, profile, join_paths, short_hash};
 
 use self::job::{Job, Work};
@@ -272,13 +272,6 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
         state.running(&rustc);
         let process_builder = rustc.into_process_builder();
         try!(if json_errors {
-            #[derive(RustcEncodable)]
-            struct Message<'a> {
-                reason: &'a str,
-                package_id: &'a PackageId,
-                target: &'a Target,
-                message: json::Json,
-            }
             process_builder.exec_with_streaming(
                 &mut |line| if !line.is_empty() {
                     Err(internal(&format!("compiler stdout is not empty: `{}`", line)))
@@ -286,18 +279,16 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
                     Ok(())
                 },
                 &mut |line| {
-                    let rustc_message = try!(json::Json::from_str(line).map_err(|_| {
+                    let compiler_message = try!(json::Json::from_str(line).map_err(|_| {
                         internal(&format!("compiler produced invalid json: `{}`", line))
                     }));
 
-                    let message = Message {
-                        reason: "rustc-message",
-                        package_id: &package_id,
-                        target: &target,
-                        message: rustc_message,
-                    };
-                    let encoded = json::encode(&message).unwrap();
-                    println!("{}", encoded);
+                    machine_message::FromCompiler::new(
+                        &package_id,
+                        &target,
+                        compiler_message
+                    ).emit();
+
                     Ok(())
                 },
             ).map(|_| ())
diff --git a/src/cargo/util/machine_message.rs b/src/cargo/util/machine_message.rs
new file mode 100644 (file)
index 0000000..7de1ead
--- /dev/null
@@ -0,0 +1,30 @@
+use rustc_serialize::json;
+use core::{PackageId, Target};
+
+#[derive(RustcEncodable)]
+pub struct FromCompiler<'a> {
+    reason: &'static str,
+    package_id: &'a PackageId,
+    target: &'a Target,
+    message: json::Json,
+}
+
+impl<'a> FromCompiler<'a> {
+    pub fn new(package_id: &'a PackageId,
+               target: &'a Target,
+               message: json::Json)
+               -> FromCompiler<'a> {
+        FromCompiler {
+            reason: "compiler-message",
+            package_id: package_id,
+            target: target,
+            message: message,
+        }
+    }
+
+    pub fn emit(self) {
+        let json = json::encode(&self).unwrap();
+        println!("{}", json);
+    }
+}
+
index 3b18fb1e573d4499b4ed9349fe899142136faf33..56bbeed0d99801d18dd5ef0e6b62565ae21a7aaa 100644 (file)
@@ -25,15 +25,16 @@ pub mod errors;
 pub mod graph;
 pub mod hex;
 pub mod important_paths;
+pub mod job;
+pub mod lev_distance;
+pub mod machine_message;
+pub mod network;
 pub mod paths;
 pub mod process_builder;
 pub mod profile;
 pub mod to_semver;
 pub mod to_url;
 pub mod toml;
-pub mod lev_distance;
-pub mod job;
-pub mod network;
 mod cfg;
 mod dependency_queue;
 mod rustc;
index 14817b167cd662f50d251f634545647da6a2d6c6..f30dc54f676547a93160f2d823b2e0ae6beaa1de 100644 (file)
@@ -2305,7 +2305,7 @@ fn compiler_json_error_format() {
                     .arg("--message-format").arg("json-v1"),
                 execs().with_json(r#"
     {
-        "reason":"rustc-message",
+        "reason":"compiler-message",
         "package_id":"bar 0.5.0 ([..])",
         "target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
         "message":{
@@ -2321,7 +2321,7 @@ fn compiler_json_error_format() {
     }
 
     {
-        "reason":"rustc-message",
+        "reason":"compiler-message",
         "package_id":"foo 0.5.0 ([..])",
         "target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
         "message":{